package com.wtwd.campus.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.campus.dao.AppDisableTimeMapper;
import com.wtwd.campus.dao.DeviceAppPublicFileMapper;
import com.wtwd.campus.entity.AppDisableTime;
import com.wtwd.campus.entity.DeviceAppPublicFile;
import com.wtwd.campus.entity.MqAppDisableTime;
import com.wtwd.campus.service.AppDisableTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-12-16
 */
@Service
public class AppDisableTimeServiceImpl extends ServiceImpl<AppDisableTimeMapper, AppDisableTime> implements AppDisableTimeService {


    @Resource
    private AppDisableTimeMapper appDisableTimeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean synchronousData(Integer deviceId, String packageName, List<MqAppDisableTime> mqAppDisableTimes) {
        if (mqAppDisableTimes == null || mqAppDisableTimes.size() == 0) {
            return deleteAppDisableByDeviceIdAndPackageName(deviceId, packageName);
        }
        List<AppDisableTime> data = getAppDisableTimesByDeviceIdAndPackageName(deviceId, packageName);
        if (data == null) {
            data = new ArrayList<>();
        }
        if (data.size() > mqAppDisableTimes.size()) {//如果数据库中的数据要多余设备回复过来的,则删除重新创建
            deleteAppDisableByDeviceIdAndPackageName(deviceId, packageName);
            data = new ArrayList<>();
        }
        List<AppDisableTime> addDisableTime = new ArrayList<>();
        ArrayList<AppDisableTime> updateDisableTimes = new ArrayList<>();
        for (int i = 0; i < mqAppDisableTimes.size(); i++) {
            MqAppDisableTime mqAppDisableTime = mqAppDisableTimes.get(i);
            AppDisableTime appDisableTime = data.get(i);
            if (appDisableTime != null) {
                appDisableTime.setStartTime(mqAppDisableTime.getStartTime());
                appDisableTime.setEndTime(mqAppDisableTime.getEndTime());
                appDisableTime.setCycle(mqAppDisableTime.getCycle());
                appDisableTime.setFlag(mqAppDisableTime.getActivation());
                appDisableTime.setUpdateTime(new Date().getTime());
                updateDisableTimes.add(appDisableTime);
            } else {//如果设备回复存在数据库中不存在则添加进去保持设备与数据库中同步
                appDisableTime = new AppDisableTime();
                appDisableTime.setDeviceId(deviceId);
                appDisableTime.setPackageName(packageName);
                appDisableTime.setStartTime(mqAppDisableTime.getStartTime());
                appDisableTime.setEndTime(mqAppDisableTime.getEndTime());
                appDisableTime.setCycle(mqAppDisableTime.getCycle());
                appDisableTime.setFlag(mqAppDisableTime.getActivation());
                appDisableTime.setCreateTime(new Date().getTime());
                appDisableTime.setUpdateTime(new Date().getTime());
                addDisableTime.add(appDisableTime);
            }
        }
        if (addDisableTime != null && addDisableTime.size() > 0) {
            saveBatch(addDisableTime);
        }
        if (updateDisableTimes != null && updateDisableTimes.size() > 0) {
            updateBatchById(updateDisableTimes);
        }
        return true;
    }

    @Override
    public List<AppDisableTime> getAppDisableTimesByDeviceIdAndPackageName(Integer deviceId, String packageName) {
        return new LambdaQueryChainWrapper<>(appDisableTimeMapper)
                .eq(AppDisableTime::getDeviceId, deviceId)
                .eq(AppDisableTime::getPackageName, packageName)
                .list();
    }

    @Override
    public boolean deleteAppDisableByDeviceIdAndPackageName(Integer deviceId, String packageName) {
        return appDisableTimeMapper.deleteAppDisableByDeviceIdAndPackageName(deviceId,packageName);
    }
}
